// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.randomizedWorld.randomizedVehicleStory;

import java.util.HashMap;
import java.util.Iterator;
import org.joml.Vector2f;
import zombie.core.Core;
import zombie.core.Rand;
import zombie.core.math.PZMath;
import zombie.iso.IsoCell;
import zombie.iso.IsoChunk;
import zombie.iso.IsoDirections;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoMetaGrid;
import zombie.iso.IsoWorld;
import zombie.iso.Vector2;
import zombie.network.GameServer;
import zombie.network.ServerMap;
import zombie.randomizedWorld.RandomizedWorldBase;
import zombie.vehicles.BaseVehicle;

public class RandomizedVehicleStoryBase extends RandomizedWorldBase {
    private int chance = 0;
    private static int totalChance = 0;
    private static HashMap<RandomizedVehicleStoryBase, Integer> rvsMap = new HashMap();
    protected boolean horizontalZone = false;
    protected int zoneWidth = 0;
    public static final float baseChance = 12.5F;
    protected int minX = 0;
    protected int minY = 0;
    protected int maxX = 0;
    protected int maxY = 0;
    protected int minZoneWidth = 0;
    protected int minZoneHeight = 0;

    public static void initAllRVSMapChance(IsoMetaGrid.Zone zone, IsoChunk chunk) {
        totalChance = 0;
        rvsMap.clear();

        for (int _int = 0; _int < IsoWorld.instance.getRandomizedVehicleStoryList().size(); _int++) {
            RandomizedVehicleStoryBase randomizedVehicleStoryBase = (RandomizedVehicleStoryBase)IsoWorld.instance.getRandomizedVehicleStoryList().get(_int);
            if (randomizedVehicleStoryBase.isValid(zone, chunk, false) && randomizedVehicleStoryBase.isTimeValid(false)) {
                totalChance = totalChance + randomizedVehicleStoryBase.getChance();
                rvsMap.put(randomizedVehicleStoryBase, randomizedVehicleStoryBase.getChance());
            }
        }
    }

    public static boolean doRandomStory(IsoMetaGrid.Zone zone, IsoChunk chunk, boolean force) {
        float _float = Rand.Next(0.0F, 500.0F);
        switch (zombie.SandboxOptions.instance.VehicleStoryChance.getValue()) {
            case 1:
                return false;
            case 2:
                _float = Rand.Next(0.0F, 1000.0F);
            case 3:
            default:
                break;
            case 4:
                _float = Rand.Next(0.0F, 300.0F);
                break;
            case 5:
                _float = Rand.Next(0.0F, 175.0F);
                break;
            case 6:
                _float = Rand.Next(0.0F, 50.0F);
        }

        if (_float < 12.5F) {
            if (!chunk.vehicles.isEmpty()) {
                return false;
            } else {
                RandomizedVehicleStoryBase randomizedVehicleStoryBase = null;
                initAllRVSMapChance(zone, chunk);
                randomizedVehicleStoryBase = getRandomStory();
                if (randomizedVehicleStoryBase == null) {
                    return false;
                } else {
                    VehicleStorySpawnData vehicleStorySpawnData = randomizedVehicleStoryBase.initSpawnDataForChunk(zone, chunk);
                    chunk.setRandomVehicleStoryToSpawnLater(vehicleStorySpawnData);
                    return true;
                }
            }
        } else {
            return false;
        }
    }

    private static RandomizedVehicleStoryBase getRandomStory() {
        int int0 = Rand.Next(totalChance);
        Iterator iterator = rvsMap.keySet().iterator();
        int int1 = 0;

        while (iterator.hasNext()) {
            RandomizedVehicleStoryBase randomizedVehicleStoryBase = (RandomizedVehicleStoryBase)iterator.next();
            int1 += rvsMap.get(randomizedVehicleStoryBase);
            if (int0 < int1) {
                return randomizedVehicleStoryBase;
            }
        }

        return null;
    }

    public int getMinZoneWidth() {
        return this.minZoneWidth <= 0 ? 10 : this.minZoneWidth;
    }

    public int getMinZoneHeight() {
        return this.minZoneHeight <= 0 ? 5 : this.minZoneHeight;
    }

    public void randomizeVehicleStory(IsoMetaGrid.Zone zone, IsoChunk chunk) {
    }

    public IsoGridSquare getCenterOfChunk(IsoMetaGrid.Zone zone, IsoChunk chunk) {
        int int0 = Math.max(zone.x, chunk.wx * 10);
        int int1 = Math.max(zone.y, chunk.wy * 10);
        int int2 = Math.min(zone.x + zone.w, (chunk.wx + 1) * 10);
        int int3 = Math.min(zone.y + zone.h, (chunk.wy + 1) * 10);
        int int4 = 0;
        int int5 = 0;
        if (this.horizontalZone) {
            int5 = (zone.y + zone.y + zone.h) / 2;
            int4 = (int0 + int2) / 2;
        } else {
            int5 = (int1 + int3) / 2;
            int4 = (zone.x + zone.x + zone.w) / 2;
        }

        return IsoCell.getInstance().getGridSquare(int4, int5, zone.z);
    }

    public boolean isValid(IsoMetaGrid.Zone zone, IsoChunk chunk, boolean force) {
        this.horizontalZone = false;
        this.zoneWidth = 0;
        this.debugLine = "";
        if (!force && zone.hourLastSeen != 0) {
            return false;
        } else if (!force && zone.haveConstruction) {
            return false;
        } else if (!"Nav".equals(zone.getType())) {
            this.debugLine = this.debugLine + "Not a 'Nav' zone.";
            return false;
        } else {
            this.minX = Math.max(zone.x, chunk.wx * 10);
            this.minY = Math.max(zone.y, chunk.wy * 10);
            this.maxX = Math.min(zone.x + zone.w, (chunk.wx + 1) * 10);
            this.maxY = Math.min(zone.y + zone.h, (chunk.wy + 1) * 10);
            return this.getSpawnPoint(zone, chunk, null);
        }
    }

    public VehicleStorySpawnData initSpawnDataForChunk(IsoMetaGrid.Zone zone, IsoChunk chunk) {
        int int0 = this.getMinZoneWidth();
        int int1 = this.getMinZoneHeight();
        float[] float0 = new float[3];
        if (!this.getSpawnPoint(zone, chunk, float0)) {
            return null;
        } else {
            float float1 = float0[0];
            float float2 = float0[1];
            float float3 = float0[2];
            int[] int2 = new int[4];
            VehicleStorySpawner.getInstance().getAABB(float1, float2, (float)int0, (float)int1, float3, int2);
            return new VehicleStorySpawnData(this, zone, float1, float2, float3, int2[0], int2[1], int2[2], int2[3]);
        }
    }

    public boolean getSpawnPoint(IsoMetaGrid.Zone zone, IsoChunk isoChunk, float[] _float) {
        return this.getRectangleSpawnPoint(zone, isoChunk, _float) || this.getPolylineSpawnPoint(zone, isoChunk, _float);
    }

    public boolean getRectangleSpawnPoint(IsoMetaGrid.Zone zone, IsoChunk isoChunk, float[] float0) {
        if (!zone.isRectangle()) {
            return false;
        } else {
            int int0 = this.getMinZoneWidth();
            int int1 = this.getMinZoneHeight();
            if (zone.w > 30 && zone.h < 15) {
                this.horizontalZone = true;
                this.zoneWidth = zone.h;
                if (zone.getWidth() < int1) {
                    this.debugLine = "Horizontal street is too small, w:" + zone.getWidth() + " h:" + zone.getHeight();
                    return false;
                } else if (zone.getHeight() < int0) {
                    this.debugLine = "Horizontal street is too small, w:" + zone.getWidth() + " h:" + zone.getHeight();
                    return false;
                } else if (float0 == null) {
                    return true;
                } else {
                    float float1 = (float)zone.getX();
                    float float2 = (float)(zone.getX() + zone.getWidth());
                    float float3 = (float)zone.getY() + (float)zone.getHeight() / 2.0F;
                    float0[0] = PZMath.clamp((float)(isoChunk.wx * 10) + 5.0F, float1 + (float)int1 / 2.0F, float2 - (float)int1 / 2.0F);
                    float0[1] = float3;
                    float0[2] = Vector2.getDirection(float2 - float1, 0.0F);
                    return true;
                }
            } else if (zone.h > 30 && zone.w < 15) {
                this.horizontalZone = false;
                this.zoneWidth = zone.w;
                if (zone.getWidth() < int0) {
                    this.debugLine = "Vertical street is too small, w:" + zone.getWidth() + " h:" + zone.getHeight();
                    return false;
                } else if (zone.getHeight() < int1) {
                    this.debugLine = "Vertical street is too small, w:" + zone.getWidth() + " h:" + zone.getHeight();
                    return false;
                } else if (float0 == null) {
                    return true;
                } else {
                    float float4 = (float)zone.getY();
                    float float5 = (float)(zone.getY() + zone.getHeight());
                    float float6 = (float)zone.getX() + (float)zone.getWidth() / 2.0F;
                    float0[0] = float6;
                    float0[1] = PZMath.clamp((float)(isoChunk.wy * 10) + 5.0F, float4 + (float)int1 / 2.0F, float5 - (float)int1 / 2.0F);
                    float0[2] = Vector2.getDirection(0.0F, float4 - float5);
                    return true;
                }
            } else {
                this.debugLine = "Zone too small or too large";
                return false;
            }
        }
    }

    public boolean getPolylineSpawnPoint(IsoMetaGrid.Zone zone, IsoChunk isoChunk, float[] float8) {
        if (zone.isPolyline() && zone.polylineWidth > 0) {
            int int0 = this.getMinZoneWidth();
            int int1 = this.getMinZoneHeight();
            if (zone.polylineWidth < int0) {
                this.debugLine = "Polyline zone is too narrow, width:" + zone.polylineWidth;
                return false;
            } else {
                double[] double0 = new double[2];
                int int2 = zone.getClippedSegmentOfPolyline(isoChunk.wx * 10, isoChunk.wy * 10, (isoChunk.wx + 1) * 10, (isoChunk.wy + 1) * 10, double0);
                if (int2 == -1) {
                    return false;
                } else {
                    double double1 = double0[0];
                    double double2 = double0[1];
                    float float0 = zone.polylineWidth % 2 == 0 ? 0.0F : 0.5F;
                    float float1 = (float)zone.points.get(int2 * 2) + float0;
                    float float2 = (float)zone.points.get(int2 * 2 + 1) + float0;
                    float float3 = (float)zone.points.get(int2 * 2 + 2) + float0;
                    float float4 = (float)zone.points.get(int2 * 2 + 3) + float0;
                    float float5 = float3 - float1;
                    float float6 = float4 - float2;
                    float float7 = Vector2f.length(float5, float6);
                    if (float7 < (float)int1) {
                        return false;
                    } else {
                        this.zoneWidth = zone.polylineWidth;
                        if (float8 == null) {
                            return true;
                        } else {
                            float float9 = (float)int1 / 2.0F / float7;
                            float float10 = PZMath.max((float)double1 - float9, float9);
                            float float11 = PZMath.min((float)double2 + float9, 1.0F - float9);
                            float float12 = float1 + float5 * float10;
                            float float13 = float2 + float6 * float10;
                            float float14 = float1 + float5 * float11;
                            float float15 = float2 + float6 * float11;
                            float float16 = Rand.Next(0.0F, 1.0F);
                            if (Core.bDebug) {
                                float16 = (float)(System.currentTimeMillis() / 20L % 360L) / 360.0F;
                            }

                            float8[0] = float12 + (float14 - float12) * float16;
                            float8[1] = float13 + (float15 - float13) * float16;
                            float8[2] = Vector2.getDirection(float5, float6);
                            return true;
                        }
                    }
                }
            }
        } else {
            return false;
        }
    }

    public boolean isFullyStreamedIn(int x1, int y1, int x2, int y2) {
        byte _byte = 10;
        int int0 = x1 / _byte;
        int int1 = y1 / _byte;
        int int2 = (x2 - 1) / _byte;
        int int3 = (y2 - 1) / _byte;

        for (int int4 = int1; int4 <= int3; int4++) {
            for (int int5 = int0; int5 <= int2; int5++) {
                if (!this.isChunkLoaded(int5, int4)) {
                    return false;
                }
            }
        }

        return true;
    }

    public boolean isChunkLoaded(int wx, int wy) {
        IsoChunk isoChunk = GameServer.bServer ? ServerMap.instance.getChunk(wx, wy) : IsoWorld.instance.CurrentCell.getChunk(wx, wy);
        return isoChunk != null && isoChunk.bLoaded;
    }

    public boolean initVehicleStorySpawner(IsoMetaGrid.Zone zone, IsoChunk chunk, boolean debug) {
        return false;
    }

    public boolean callVehicleStorySpawner(IsoMetaGrid.Zone zone, IsoChunk chunk, float additionalRotationRadians) {
        float[] float0 = new float[3];
        if (!this.getSpawnPoint(zone, chunk, float0)) {
            return false;
        } else {
            this.initVehicleStorySpawner(zone, chunk, false);
            VehicleStorySpawner vehicleStorySpawner = VehicleStorySpawner.getInstance();
            float float1 = float0[2];
            if (Rand.NextBool(2)) {
                float1 += (float) Math.PI;
            }

            float1 += additionalRotationRadians;
            vehicleStorySpawner.spawn(float0[0], float0[1], 0.0F, ++float1, this::spawnElement);
            return true;
        }
    }

    public void spawnElement(VehicleStorySpawner spawner, VehicleStorySpawner.Element element) {
    }

    public BaseVehicle[] addSmashedOverlay(BaseVehicle v1, BaseVehicle v2, int xOffset, int yOffset, boolean _horizontalZone, boolean addBlood) {
        IsoDirections isoDirections0 = v1.getDir();
        IsoDirections isoDirections1 = v2.getDir();
        Object object0 = null;
        Object object1 = null;
        if (!_horizontalZone) {
            object0 = "Front";
            if (isoDirections1 == IsoDirections.W) {
                if (isoDirections0 == IsoDirections.S) {
                    object1 = "Right";
                } else {
                    object1 = "Left";
                }
            } else if (isoDirections0 == IsoDirections.S) {
                object1 = "Left";
            } else {
                object1 = "Right";
            }
        } else {
            if (isoDirections0 == IsoDirections.S) {
                if (xOffset > 0) {
                    object0 = "Left";
                } else {
                    object0 = "Right";
                }
            } else if (xOffset < 0) {
                object0 = "Left";
            } else {
                object0 = "Right";
            }

            object1 = "Front";
        }

        v1 = v1.setSmashed((String)object0);
        v2 = v2.setSmashed((String)object1);
        if (addBlood) {
            v1.setBloodIntensity((String)object0, 1.0F);
            v2.setBloodIntensity((String)object1, 1.0F);
        }

        return new BaseVehicle[]{v1, v2};
    }

    public int getChance() {
        return this.chance;
    }

    public void setChance(int _chance) {
        this.chance = _chance;
    }

    public int getMinimumDays() {
        return this.minimumDays;
    }

    public void setMinimumDays(int minimumDays) {
        this.minimumDays = minimumDays;
    }

    @Override
    public int getMaximumDays() {
        return this.maximumDays;
    }

    @Override
    public void setMaximumDays(int maximumDays) {
        this.maximumDays = maximumDays;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public String getDebugLine() {
        return this.debugLine;
    }

    public void registerCustomOutfits() {
    }
}
